import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

public class Util {

    /**
     * @param response
     * @param filePath     生成文件的路径
     * @param showFileName 显示的文件名
     * @throws IOException
     */
    protected static void previewpdf(HttpServletResponse response, String filePath, String showFileName) throws IOException {
        File f = new File(filePath);
        if (!f.isFile() || !f.exists()) {// 文件异常
            throw new FileNotFoundException("文件异常,请检查文件是否正确:" + filePath);
        }
        // 获取文件流
        InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        response.reset();
        // 设置response的Header
        response.setCharacterEncoding("UTF-8");
        //response.setContentType("application/octet-stream;charset=UTF-8");
        //response.addHeader("Content-Disposition", "attachment1;filename=" + new String(showFileName.getBytes("gb2312"), "ISO8859-1"));
        response.setContentType("application/pdf;charset=UTF-8");
        // 下载文件名处理
        response.addHeader("Content-Disposition", "inline;filename=" + new String(showFileName.getBytes("gb2312"), "ISO8859-1"));
        response.addHeader("Content-Length", "" + f.length());
        // 文件流处理
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

        toClient.write(buffer);
        toClient.flush();
        toClient.close();
        //删除临时文件
        File file = new File(filePath);
        file.delete();
    }


    /**
     * @param templatePath 模板路径
     * @param newPDFPath   新文件路径
     * @param map
     * @throws IOException
     */
    public static void interviewReportPDF( String templatePath, String newPDFPath, Map<String, String> map) throws IOException {
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            PdfContentByte content = stamper.getOverContent(reader.getNumberOfPages());
            Document doc = new Document(reader.getPageSize(1));
            float absolutePositionNewY = doc.getPageSize().getHeight() - 250;
            // X轴位置的值
            float absolutePositionNewX = 80;

            FileOutputStream out1 = new FileOutputStream("F:\\2020.pdf");
            PdfCopy pdfCopy = new PdfCopy(doc, out1);
            doc.open();
            PdfImportedPage page = pdfCopy.getImportedPage(reader, 1);
            pdfCopy.addPage(page);

            BarcodeQRCode barcodeQRCode = new BarcodeQRCode("barcode test", 1000, 1000, null);
            Image codeQrImage = barcodeQRCode.getImage();
            codeQrImage.scaleAbsolute(100, 100);
            codeQrImage.setAbsolutePosition(absolutePositionNewX, absolutePositionNewY);// 二维码的位置

//            pdfCopy.add;

            doc.close();
//            PdfWriter.getInstance(doc,out);
//            doc.open();
//            doc.add(new Paragraph("Hello World!"));

//            doc.close();
            // 获取页面高度


            // 给表单添加中文字体 这里采用系统字体。不设置的话，中文可能无法显示
            BaseFont bf = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

			/*BaseFont bf = BaseFont.createFont(UtilPath.getRootPath() + "fonts/simsun.ttc,0", BaseFont.IDENTITY_H,
					BaseFont.EMBEDDED);*/
            form.addSubstitutionFont(bf);
            stamper.setFormFlattening(false);

            //遍历map装入数据
            for (Map.Entry<String, String> entry : map.entrySet()) {
                form.setField(entry.getKey(), entry.getValue());
            }


            stamper.setEncryption(null,null,PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);


//
//            Barcode128 barcode128 = new Barcode128();
//            barcode128.setSize(10);
//            barcode128.setBarHeight(35);
//
//            barcode128.setCode("this is a text message");
//            barcode128.setStartStopText(false);
//            barcode128.setExtended(true);
//
//            Image image128 = barcode128.createImageWithBarcode(content, null, null);
//            image128.setAbsolutePosition(absolutePositionNewX + 150, absolutePositionNewY);
//
//
//            content.addImage(codeQrImage);
//            content.addImage(image128);
//            doc.add(codeQrImage);
            stamper.close();


//            Document doc = new Document();
//            PdfWriter writer = PdfWriter.getInstance(doc, out);
//           writer.setEncryption(null, null, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
//            doc.open();




//            doc.add(codeQrImage);
//            doc.add(stamper)

//
                out.write(bos.toByteArray());
                out.close();
//            doc.close();

        } catch (IOException e) {
            System.out.println("error");
        } catch (DocumentException e) {
            System.out.println("error1");
        }
        //到页面上进行展示
//        previewpdf(response, newPDFPath, showFileName);

    }


}
